﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace Itfort.Install
{
	/// <summary>
	/// 
	/// </summary>
	public class ServiceControllerEx
	{
		private string m_ServiceName;
		private Action m_RunAsConsole;
		private Installer m_Installer;

		/// <summary>
		/// 
		/// </summary>
		public string ServiceName
		{
			get
			{
				return m_ServiceName;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="serviceName"></param>
		/// <param name="runAsConsole"></param>
		public ServiceControllerEx(string serviceName, Action runAsConsole)
		{
			m_ServiceName = serviceName;
			m_RunAsConsole = runAsConsole;
			m_Installer = new InstallerEx(m_ServiceName);
		}

		/// <summary>
		/// 
		/// </summary>
		public void Install()
		{
			Hashtable hashtable = new Hashtable();
			m_Installer.Install(hashtable);
		}

		/// <summary>
		/// 
		/// </summary>
		public void Uninstall()
		{
			m_Installer.Uninstall(null);
		}

		/// <summary>
		/// 
		/// </summary>
		public void StartService()
		{
			ServiceController sc = ServiceUtil.LookupService(this.ServiceName);
			if (sc != null)
			{
				if (sc.Status == ServiceControllerStatus.Stopped)
				{
					sc.Start();
					Console.WriteLine(String.Format("服务{0}已启动。", this.ServiceName));
				}
				else if (sc.Status == ServiceControllerStatus.Running)
				{
					Console.WriteLine(String.Format("服务{0}正在运行中，无需启动。", this.ServiceName));
				}
				else
				{
					Console.WriteLine(String.Format("服务{0}状态异常，请检查并重试", this.ServiceName));
				}

			}
			else
			{
				Console.WriteLine(String.Format("服务{0}未安装，请先安装服务。", this.ServiceName));
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void StopService()
		{
			ServiceController sc = ServiceUtil.LookupService("Caifu.SpidersCenter");
			sc.Stop();
		}

		/// <summary>
		/// 
		/// </summary>
		public void StartConsole()
		{
			if (m_RunAsConsole != null)
			{
				m_RunAsConsole();
				Console.WriteLine("按回车键结束");
				Console.ReadLine();
			}
			else
			{
				throw new NullReferenceException("RunAsConsole不能为空。");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="args"></param>
		public void Execute(string[] args)
		{
			try
			{
				// 命令
				var cmd = args[0];
				cmd = cmd.ToLower().Replace('-', '/');

				switch (cmd)
				{
					case "/install":
					case "/i":				// 安装
						Install();
						break;
					case "/uninstall":
					case "/u":				// 卸载
						Uninstall();
						break;
					case "/start":
					case "/s":				// 启动
						StartService();
						break;
					case "/stop":
					case "/e":				// 停止
						StopService();
						break;
					case "/console":
					case "/c":				// 控制台启动
						StartConsole();
						break;
				}
			}
			catch (Exception ex)
			{
				TraceUtil.Write(ex);
			}
		}
	}
}
